<template>
  <div class="home settingAttendance">
    <div class="settingAttendance-form">
      <el-form
        :model="attendanceInfo"
        label-width="120px"
        :inline="true"
        :rules="attendanceRules"
      >
        <!-- <el-row> -->
        <!-- <el-col :span="8" style="text-align: right"> -->
        <el-form-item label="考勤日期：" prop="date">
          <el-date-picker
            v-model="attendanceInfo.date"
            type="date"
            placeholder="选择考勤日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker> </el-form-item
        ><br />
        <!-- </el-col> -->
        <!-- <el-col :span="8" style="text-align: right"> -->
        <el-form-item label="考勤开始时间：" prop="startTime">
          <el-time-picker
            v-model="attendanceInfo.startTime"
            placeholder="选择开始时间"
            value-format="HH:mm:ss"
          >
          </el-time-picker>
        </el-form-item>
        <!-- </el-col> -->
        <!-- <el-col :span="8" style="text-align: left"> -->
        <el-form-item label="考勤结束时间：" prop="endTime">
          <el-time-picker
            v-model="attendanceInfo.endTime"
            placeholder="选择结束时间"
            value-format="HH:mm:ss"
          >
          </el-time-picker>
        </el-form-item>
        <!-- </el-col> -->
        <!-- </el-row> -->
      </el-form>
    </div>
    <div class="table">
      <!-- :data="addPersonnelList" -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" width="55" label="序号" align="center">
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="name"
          label="姓名"
        >
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="age"
          label="年龄"
        >
        <template slot-scope="scope">
            <div>{{scope.row.birthDate|birthToAge}}</div>
          </template>
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="phone"
          label="电话"
        >
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="workTypeName"
          label="工种"
        >
        </el-table-column>
        <el-table-column header-align="center" align="center" label="操作">
          <template slot-scope="scope">
            <div>
              <el-button
                type="text"
                @click="deleteItem(scope.$index)"
                icon="el-icon-delete"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- <div class="block-space"></div> -->
      <el-row class="page">
        <div class="page-tag">
          共{{ Math.ceil(totalData / pageSize) }}页/{{ totalData }}条数据
        </div>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-sizes="[10, 20, 50]"
          :page-size="pageSize"
          layout="sizes, prev, pager, next, jumper"
          :total="totalData"
        >
          >
        </el-pagination>
      </el-row>
    </div>
    <div
      class="main-title row"
      style="
        background-color: #ffffff;
        border-top: solid 1px #e4e4e4;
        margin-top: 10px;
      "
    >
      <div style="width: 100%; text-align: right; margin-right: 20px">
        <el-button class="sub-btn" type="info" @click="cancle">上一步</el-button>
        <el-button class="sub-btn color-1abc9c" @click="next">下一步</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import eventBus from "../eventBus";
import { getAge} from "@/api/attendance";
export default {
  props: ["selectecList"],
  data() {
    return {
      attendanceInfo: {
        startTime: "",
        endTime: "",
        date: null,
      },
      modeOptions: [
        { value: 1, label: "按小时" },
        { value: 2, label: "按天" },
      ],
      tableData: [],
      attendanceRules: {
        date: [{ required: true, message: "请选择考勤日期", trigger: "blur" }],
        startTime: [
          { required: true, message: "请选择考勤开始日期", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "请选择考勤结束日期", trigger: "blur" },
        ],
      },
      selectedList: [],
      currentPage: 1,
      // totalPage:50,
      pageSize: 10,
      totalData: 0,
    };
  },
  filters:{
    birthToAge(birth){
      return getAge(birth)
    }
  },
  mounted() {
    // 通过事件总线监听消息
    eventBus.$on("selectecList", (res) => {
      // console.log("personnelList"+res);
      this.tableData = res;
      this.totalData = res.length;
    });
  },
  //   computed: {
  //   switchtableData: function () {
  //    return this.tableData // 监听switchStatusData 的变化
  //   }
  //  },
  // watch: {
  //   personnelList(newV, oldV) {
  //     // watch监听props里status的变化，然后执行操作
  //     //  console.log(newV, oldV)
  //     this.tableData = newV;
  //   },
  // },
  methods: {
    cancle() {
      this.$emit("getActive", 0);
    },
    next() {
      let message = "";
      if (this.attendanceInfo.date == null) {
        // this.$emit("getActive",2)
        message = "请选择考勤日期";
      } else if (this.attendanceInfo.startTime == "") {
        // this.$emit("getActive",2)
        message = "请选择考勤开始时间";
      } else if (this.attendanceInfo.endTime == "") {
        message = "请选择考勤结束时间";
      } else {
        this.$emit("getActive", 2);
        // 通过事件总线发送消息
        // console.log(this.attendanceInfo)
        let array = [];
        this.tableData.map((item, index) => {
          array.push(
            Object.assign(item, {
              endTime: this.attendanceInfo.endTime,
              date: this.attendanceInfo.date,
              startTime: this.attendanceInfo.startTime,
            })
          );
        });
        eventBus.$emit("attendanceInfo", this.attendanceInfo);
        eventBus.$emit("personnelList", this.tableData);
      }
      if (message != "") {
        this.$message({
          type: "warning",
          message: message,
        });
      }
    },
    //   获取当前页数据
    getCPageData(data) {
      //   getPaymentList(data).then((res) => {
      // this.totalData = res.data.data.total;
      // console.log(res)
      // this.tableData = res.data.data.records;
      //   });
    },
    handleSizeChange() {},
    //更改当前页
    handleCurrentChange() {
      // console.log(this.currentPage);
      this.getCPageData({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      });
    },
    deleteItem(index) {
      this.tableData.splice(index, 1);
    },
  },
};
</script>

<style lang="scss" scoped>
.settingAttendance {
  padding-top: 20px;
  background-color: #ffffff;
  text-align: center;
  padding-bottom: 40px;
  .settingAttendance-form {
    width: 80%;
    margin: 0 auto;
    .el-form {
      text-align: left;
    }
  }
  .table {
    width: 80%;
    margin: 0 auto;
    margin-top: 40px;
    // border: solid 1px #e4e4e4;
    position: relative;
    .block-space {
      width: 100%;
      height: 50px;
    }
    .list-block {
      position: absolute;
      bottom: 0;
      width: 100%;
      height: 50px;
      text-align: right;
      background-color: #ffffff;
      padding-top: 10px;
      //   border: solid 1px #e4e4e4;
      padding-right: 10px;
      justify-content: flex-end;
      .page-tag {
        font-size: 13px;
        line-height: 30px;
        min-width: 120px;
      }
    }
  }
}
</style>